package com.cheyunkeji.er.activity.evaluate;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.adapter.evaluate.SelectCarByVinResultAdapter;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.evaluate.CarInfoSearchedByVin;
import com.cheyunkeji.er.view.SToast;
import com.cheyunkeji.er.view.TopBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ArchiveSelectCarActivity extends BaseActivity
    implements SelectCarByVinResultAdapter.onItemCBoxRecheckListener,
        AdapterView.OnItemClickListener {

  private static final String TAG = ArchiveSelectCarActivity.class.getSimpleName();

  @BindView(R.id.rl_content)
  RelativeLayout rlContent;

  @BindView(R.id.v_topbar)
  TopBar vTopbar;

  @BindView(R.id.tv_confirm)
  TextView tvConfirm;

  @BindView(R.id.tv_cancel)
  TextView tvCancel;

  @BindView(R.id.ll_bottom_opt)
  LinearLayout llBottomOpt;

  @BindView(R.id.cb_add)
  CheckBox cbAdd;

  @BindView(R.id.lv_content)
  ListView lvContent;

  @BindView(R.id.hori_scroll_view)
  HorizontalScrollView horiScrollView;

  @BindView(R.id.activity_archive_select_car)
  LinearLayout activityArchiveSelectCar;

  private int CHECKED_POSITION = -1;
  private SelectCarByVinResultAdapter adapter;
  private ArrayList<CarInfoSearchedByVin> dataList;

  @Override
  protected void init() {

    setContentView(R.layout.er_activity_archive_select_car);
    ButterKnife.bind(this);

    dataList = new ArrayList<>();
    adapter = new SelectCarByVinResultAdapter(dataList, ArchiveSelectCarActivity.this);
    lvContent.setAdapter(adapter);
    lvContent.setOnItemClickListener(this);
  }

  @Override
  protected void initView() {

    vTopbar.setTitle("选择车辆");
    vTopbar.setLeftBack();

    tvConfirm.setOnClickListener(this);
    tvCancel.setOnClickListener(this);
  }

  @Override
  protected void initData() {

    if (null != getIntent().getSerializableExtra("list")) {
      dataList.addAll((ArrayList<CarInfoSearchedByVin>) getIntent().getSerializableExtra("list"));
      fillDataToList(dataList);
    }
  }

  private void fillDataToList(List<CarInfoSearchedByVin> data) {

    rlContent.setVisibility(View.VISIBLE);
    //        dataList.clear();
    //        dataList.addAll(data);
    adapter.notifyDataSetChanged();
  }

  @Override
  public void onItemChecked(int position) {

    CHECKED_POSITION = position;
  }

  @Override
  public void onClick(View v) {

    switch (v.getId()) {
      case R.id.tv_confirm:
        if (CHECKED_POSITION == -1) {
          SToast.show("请选择车辆！");
        } else {
          Intent intent = new Intent();
          intent.putExtra("CAR_ARCHIVE", dataList.get(CHECKED_POSITION));
          setResult(RESULT_OK, intent);
          MyApplication.getInstance().removeActivity(ArchiveSelectCarActivity.class);
          finish();
        }

        break;

      case R.id.tv_cancel:
        MyApplication.getInstance().removeActivity(ArchiveSelectCarActivity.class);
        finish();
        break;

      default:
        break;
    }
  }

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    adapter.setLAST_CHECKED_POSITION(position);
    adapter.notifyDataSetChanged();
  }
}
